package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.dto.Setting;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.model.SmkAppUser;
import com.jservice.model.SmkRecharge;
import com.jservice.web.smkSn.SnService;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2017 - 账户充值
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2017")
@Transactional
public class Upp2017 extends BaseService implements IService{

    private String SERVER_NAME = "upp.trade.app.recharge";
    @Resource
    private SnService snService;

    @Resource
    private Sequence sequence;

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap =  request.getData();
        String card_no = paramsStringFilter(paramMap.get("card_no"));
        String total_amount = paramsStringFilter(paramMap.get("total_amount"));
        String pay_type = paramsStringFilter(paramMap.get("pay_type"));
        String account_type = paramsStringFilter(paramMap.get("account_type"));
        String trade_type = paramsStringFilter(paramMap.get("trade_type"));
        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null){
            return renderFAIL(MsgCode.Code_1007,response,header);
        }
        Map data = new HashMap();
        String out_trade_no = snService.generate(SnService.Type.order);
        data.put("id_type","01");
        data.put("id_no",smkAppUser.getIdCard());
        data.put("out_trade_no",out_trade_no);
        data.put("id_type","01");
        data.put("id_no",smkAppUser.getIdCard());
        data.put("card_no", card_no);
        data.put("total_amount",total_amount);
        data.put("account_type",account_type);
        data.put("pay_type",pay_type);//ALIPAY	支付宝 WECHAT	微信 BALANCE_RECHARGE	余额充值(取消)
        data.put("notify_url", Setting.getConfig("smkRecharge_notify_url"));
        data.put("trade_type",trade_type);

        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME,request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            //充值订单
            SmkRecharge smkRecharge = new SmkRecharge();
            smkRecharge.setId(sequence.nextIdStr());
            smkRecharge.setSn(out_trade_no);
            smkRecharge.setAmount(total_amount);
            smkRecharge.setCardNo(card_no);
            smkRecharge.setCreateDate(new Date());
            smkRecharge.setModifyDate(new Date());
            smkRecharge.setRechargeMethod(pay_type);
            smkRecharge.setTradeType(trade_type);
            smkRecharge.setTradeNo(jsonObject.getString("trade_no"));
            smkRecharge.setStatus("3");// 处理中
            smkRecharge.save();
            return renderSUC(jsonObject,response,header);
        }else{
            return renderFAIL(MsgCode.Code_1009,"市民卡网关返回异常:"+jsonObject.getString("msg"),response,header);
        }

    }
}
